<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Assets;
use App\Admin\Repositories\User;
use App\Models\UserTitle;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(['title:id,title','identity:user_id,identity']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('avatar')->image('',80,80);
            $grid->column('nickname');
            $grid->column('title.title','称号');
            $grid->column('phone');

            $grid->column('identity.identity','身份')
                ->using([1  => '讲师',2 => '顾问',3 => '名人'])
                ->label([1 => 'info',2 => 'primary',3 => 'success']);

            $grid->column('auth','认证方式')
                ->using([0  => '未认证',1 => '个人认证',2 => '企业认证'])
                ->label([0 => 'danger',1 => 'primary',2 => 'success']);

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->fixColumns(1);

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->prepend(new Assets());
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();

                $filter->like('nickname')->width(3);
                $filter->equal('phone')->width(3);
                $filter->equal('user_title_id')
                       ->select(UserTitle::getOption())
                       ->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('user_grade_id');
            $show->field('nickname');
            $show->field('avatar');
            $show->field('points');
            $show->field('phone');
            $show->field('sex');
            $show->field('user_title_id');
            $show->field('balance');
            $show->field('continuous_sign_num');
            $show->field('total_points');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->image('avatar')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->required();
            $form->text('nickname')
                 ->maxLength(60)
                 ->required();

            $form->select('user_title_id')
                ->options(UserTitle::getOption())
                ->required();

            $form->mobile('phone')
                 ->required();
            $form->display('points');


            $form->currency('balance')
                 ->symbol('￥')
                 ->readOnly();
            $form->display('continuous_sign_num');
            $form->display('total_points');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
